package com.example.invoker.schedule.flurry.others;

import cn.hutool.json.JSONObject;
import com.biapp.appboard.mapper.installation.OtherTimesMapper;
import com.biapp.appboard.pojo.model.flurry.FlurryFiltersInfo;
import com.biapp.appboard.pojo.model.flurry.FlurryOtherTimesTypeEum;
import com.biapp.appboard.pojo.model.installation.OtherTimes;
import com.biapp.appboard.service.schedule.flurry.EventParamsSyncService;
import com.biapp.common.util.ZonedDateUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author huaiyu yin
 * @Date 2021/7/6 5:00 下午
 * @Version 1.0
 */
@Service
public class SoundLidSyncService extends EventParamsSyncService {
    /**
     * 1.获取flurry平台数据
     * 2.验证数据
     * 3.组装完整的othertimes数据
     * 4.数据处理
     */


    @Autowired
    private OtherTimesMapper otherTimesMapper;

    @Override
    public Map<String, Object> getParamsMap() {
        Map<String, Object> paramsMap = new HashMap<>();
        paramsMap.put("metrics", getMetrics());
        paramsMap.put("dateTime", startDate + "/" + endDate);

        FlurryFiltersInfo filtersInfo = new FlurryFiltersInfo();
        filtersInfo.setEventName("use_count");
        filtersInfo.setParamName("apply_sound_lib");

        paramsMap.put("filters", filtersInfo.getFilters());
        return paramsMap;
    }

    @Override
    public Collection assemblyData(List<JSONObject> jsonObjectList) {
        if (CollectionUtils.isEmpty(jsonObjectList)) {
            return Collections.emptyList();
        }
        // 按天统计次数（1.先根据时间分组将jsonObjectList转为Map<String,List<jsonObjectList>> ,遍历map,统计每个时间的次数）
        Map<String, Integer> dailyCountList = jsonObjectList.stream().collect(Collectors.groupingBy(jsonObject -> jsonObject.getStr("dateTime"))).entrySet().stream().collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue().stream().mapToInt(item -> item.getInt("count")).sum()));
        List<OtherTimes> insertList = dailyCountList.entrySet().stream().map(e -> {
            String date = e.getKey().substring(0, 10);

            OtherTimes otherTimes = new OtherTimes();
            otherTimes.setDailyDate(Integer.parseInt(date.replaceAll("-", "")));
            otherTimes.setTypes(FlurryOtherTimesTypeEum.APPLY_SOUND_LIB.getType());
            otherTimes.setUseNum(e.getValue());
            otherTimes.setDailyYear(Integer.parseInt(date.substring(0, 4)));
            otherTimes.setDailyMonth(Integer.parseInt(ZonedDateUtil.getMonth(date)));
            otherTimes.setDailyWeek(Integer.parseInt(ZonedDateUtil.getWeek(date).replaceAll("W","")));
            return otherTimes;
        }).sorted(Comparator.comparing(OtherTimes::getDailyDate)).collect(Collectors.toList());

        return insertList;
    }


    @Override
    public void save(Collection entityList) {
        otherTimesMapper.batchInsert((List<OtherTimes>) entityList);
    }


}
